var readline = require('linebyline');
var fileName = 'siteActivity';
var xmlBuilder = require('xmlbuilder');
var fs = require('fs');
require('date-format-lite');


var generate = module.exports = function (fileName, desFolder, desFileName) {
    if (!fileName) {
        return;
    }
    var date = new Date().format('YYYY-MM-DD');
    var rl = readline(fileName);
    var begin = xmlBuilder.begin();

    var root = begin.ele('urlset', {
        'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'xmlns:mobile': 'http://www.baidu.com/schemas/sitemap-mobile/1/'
    });

    rl.on('line', function (line, lineCount, byteCount) {
        var url = root.ele('url');
        url.ele('mobile:mobile', {
            type: 'pc,mobile'
        });
        url.ele('loc', line);
        url.ele('changefreq', 'daily');
        url.ele('priority', '0.8');
        url.ele('lastmod', date);

    }).on('close', function (e) {
        root.end({pretty: true});
        console.log(root.toString());
        fs.writeFile(desFolder + desFileName, root.toString(), function (err) {
            if (err) throw err;
            console.log(fileName);
        });
    }).on('error', function (e) {
        // something went wrong
    });
};

